from DjangoSite.kburndown.models import Project, Unit, Task
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import F
from django.utils.datetime_safe import date

def ping(request, data):
    print "[ping: " + data.__str__() + "]"
    return "pong: " + data


##################################################
#
#  KBurndownService : Basic
#
##################################################

def get_project_data(request):
    print "[get_project_data]"
    result = Project.objects.order_by('-id')
    if result.count() == 0:
        return None
    else:
        return result[0]



def add_unit(request, name, member_count):
    print "[add_unit: name=" + name + ", member_count= " + member_count.__str__() + "]"
    unit = Unit(name=name, \
                member_count=member_count)
    unit.save()
    return unit.id
    


def get_units(request):
    print "[get_units]"
    return Unit.objects.values('id','name','member_count','style', 'pos_x', 'pos_y')



def move_unit(request, unit_id, pos_x, pos_y):
    print "[move_units: unit_id=" + unit_id.__str__() + "]"
    try:
        unit = Unit.objects.get(pk=unit_id)
    except ObjectDoesNotExist:
        return False
    
    unit.pos_x = pos_x
    unit.pos_y = pos_y
    unit.save()
    return True 



def get_tasks(request):
    print "[get_tasks]"
    return Task.objects.select_related() 



def get_tasks_by_unit(request, unit_id):
    print "[get_tasks_by_unit: unit_id=" + unit_id.__str__() + "]"
    return Task.objects.filter(owner__id=unit_id)



def add_task(request, name, content, icon, owner_id, duration):
    print "[add_task: name=" + name + ", owner_id=" + owner_id.__str__() + ", duration=" + duration.__str__() + "]"
    try:
        owner = Unit.objects.get(pk=owner_id)
    except ObjectDoesNotExist:
        return False
    return add_task_at(request, name, content, icon, owner_id, duration, owner.max_priority)



def add_task_at(request, name, content, icon, owner_id, duration, position):
    print "[add_task_at: name=" + name + ", owner_id=" + owner_id.__str__() + ", duration=" + duration.__str__() + ", position=" + position.__str__() + "]"
    try:
        owner = Unit.objects.get(pk=owner_id)
    except ObjectDoesNotExist:
        return False
    if position > owner.max_priority: return False
    
    Task.objects.filter(owner__id=owner_id, priority__gt=position, deleted=False).update(priority=F('priority') + 1)
    
    task = Task(name=name, \
                content=content, \
                icon=icon, \
                owner=owner, \
                duration=duration, \
                priority=position + 1) 
    task.save()
         
    owner.max_priority += 1
    owner.save()
    return task.id



def edit_task(request, task_id, name, content, icon, duration):
    print "[edit_task: task_id=" + task_id.__str__() + ", name=" + name + ", content=" + content + ", duration=" + duration.__str__() + "]"
    try:
        task = Task.objects.get(pk=task_id)
    except ObjectDoesNotExist:
        return False
    if task.finish_date != None or task.deleted == True: return False
    
    task.name = name
    task.content = content
    task.icon = icon
    task.duration = duration
    task.save()
    return task



def finish_task(request, task_id, op_date):
    print "[finish_task: task_id=" + task_id.__str__() + "]"
    try:
        task = Task.objects.get(pk=task_id)
    except ObjectDoesNotExist:
        return False
    if task.finish_date != None or task.deleted == True: return False
    
    owner = task.owner
    owner.max_done_priority += 1
    owner.save()
    
    task.priority = -owner.max_done_priority
    task.finish_date = op_date
    task.save()
    return True



def reopen_task(request, task_id, anchor_task_id):
    print "[reopen_task: task_id=" + task_id.__str__() + "]"
    try:
        task = Task.objects.select_related().get(pk=task_id)
    except ObjectDoesNotExist:
        return False
    if task.finish_date == None or task.deleted == True: return False
    
    owner = task.owner
    owner.max_priority += 1
    owner.save()
    
    task.finish_date = None
    task.priority = owner.max_priority
    move_task(request, task_id, anchor_task_id)
    return True



def move_task(request, task_id, anchor_task_id):
    print "[move_task: task_id=" + task_id.__str__() + ", anchor_task_id=" + anchor_task_id.__str__() + "]"
    if task_id == anchor_task_id: return True
    try:
        task = Task.objects.get(pk=task_id)
    except ObjectDoesNotExist:
        return False
    if task.finish_date != None or task.deleted == True: return False
    anchor_task = None
    if anchor_task_id != None:
        try:
            anchor_task = Task.objects.get(pk=anchor_task_id)
        except ObjectDoesNotExist:
            return False
        if anchor_task.finish_date != None or anchor_task.deleted == True: return False
    if anchor_task != None:
        if task.owner != anchor_task.owner: return False
    
    task_priority = task.priority
    if anchor_task != None:
        anchor_priority = anchor_task.priority
    else:
        anchor_priority = 0
        
    forward = (anchor_priority > task_priority)
    
    if forward == True:
        Task.objects.filter(priority__gt=task_priority, priority__lte=anchor_priority).update(priority=F('priority') - 1)        
        task.priority = anchor_priority
        task.save()
    else:
        Task.objects.filter(priority__gt=anchor_priority, priority__lt=task_priority).update(priority=F('priority') + 1)
        task.priority = anchor_priority + 1
        task.save()
    
    return True



def delete_task(request, task_id, op_date):
    print "[delete_task: task_id=" + task_id.__str__() + "]"
    try:
        task = Task.objects.get(pk=task_id)
    except ObjectDoesNotExist:
        return False
    if task.finish_date != None or task.deleted == True: return False
    
    task.deleted = True
    task.finish_date = op_date
    task.save()
    
    return True
    


##################################################
#
#  KBurndownService : Project Setup
#
##################################################

